package cn.it62.pojo;


import cn.it62.utils.Utils;

import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;


/**
 * @author zhenhuaShen
 * @version 1.0
 * @date 2019/8/7
 * @description cn.it62.pojo
 */
public class PersonMsg implements PersonOption  {
    private static List<Person> list = new ArrayList<Person>();

    static {
        list.add(new Person("it62_001", 22,"tom1"));
        list.add(new Person("it62_002", 22, "tom2"));
        list.add(new Person("it62_003", 22, "tom3"));
        list.add(new Person("it62_004", 22, "tom4"));
        list.add(new Person("it62_005", 22, "tom5"));
    }

    public void showList() {
        for (Person person : list) {
            System.out.println(person);
        }
    }

    public void showList1() {
        for (int i = 0, len = list.size(); i < len; i++) {
            System.out.println(list.get(i));
        }
    }

    public void showList2() {
        for (Iterator<Person> iterator = list.iterator(); iterator.hasNext(); ) {
            Person person = iterator.next();
            System.out.println(person);
        }
    }

    public void service() {
        while (true) {
            System.out.println("-----------------PersonMsg--------------------");
            System.out.println("\t1、查看人员");
            System.out.println("\t2、添加人员");
            System.out.println("\t3、修改人员");
            System.out.println("\t4、删除人员");
            System.out.println("\t5、退出系统");
            System.out.println("----------------------------------------------");
            int num = 0;
            try {
                num = Utils.getUtils().formatInteger("请选择编号：", 1, 5);
            } catch (Exception e) {
                e.printStackTrace();
            }
            switch (num) {
                case 1: {
                    showList2();
                    break;
                }
                case 2: {
                    System.out.println("请输入用户id：");
                    String id = Utils.sc.next();
                    int age = Utils.getUtils().formatInput("请输入用户年龄：", 18, 35);
                    System.out.println("请输入用户姓名：");
                    String useName = Utils.sc.next();
                    Person person = new Person(id, age, useName);
                    boolean flag = add(person);
                    /**
                     * 接收add方法的返回的值 true为添加成功  false为添加失败
                     */
                    if (flag) {
                        System.out.println("添加成功");
                    } else {
                        System.out.println("添加失败");
                    }
                    break;
                }
                case 3: {
                    System.out.println("请输入要修改用户的id：");
                    String id = Utils.sc.next();
                    int age = Utils.getUtils().formatInput("请输入用户年龄：", 18, 35);
                    System.out.println("请输入用户姓名：");
                    String useName = Utils.sc.next();
                    Person person = new Person(id, age, useName);
                    Person person1 = updateById(person);
                    /**
                     * 接收updateById方法返回的person对象若为null则说明修改失败反之则修改成功，输出修改前的值
                     */
                    if (person1 == null) {
                        System.out.println("修改失败！");
                    } else {
                        System.out.println("修改前的数据为：" + person1);
                        System.out.println("修改成功！");
                    }
                    break;
                }
                case 4: {
                    System.out.println("输入要删除用户的id：");
                    String id = Utils.sc.next();
                    Person person = deleteById(id);

                    if (person == null) {
                        System.out.println("删除失败！");
                    } else {
                        System.out.println("删除的数据为：" + person);
                        System.out.println("删除成功！");
                    }
                    break;
                }
                case 5: {
                    System.out.println("退出！");
                    System.exit(0);
                }
                default: {
                    System.out.println("没有" + num + "选项！");
                    break;
                }
            }
        }
    }

    /**
     * 通过id获得一个Person对象
     ** @param id
     * @return
     */
    @Override
    public int getPersonById(String id) {
        int index = -1;
        if (list.isEmpty()) {
            System.out.println("集合是空的！");
        } else {
            for (int i = 0, len = list.size(); i < len; i++) {
                Person person = list.get(i);
                if (person.getId().equals(id)) {
                    index = i;
                    return index;
                }
            }
            System.out.println("没有你要找的id！");
        }
        return index;
    }


    /**
     * 添加人的方法
     * @param person true添加成功，false添加失败
     * @return
     */
    @Override
    public boolean add(Person person) {

        int index = getPersonById(person.getId());
        if (index < 0) {
            list.add(person);
            return true;
        }
        return false;
    }

    /**
     * 修改人员
     * @param person 携带指定的id和修改后的数据
     * @return
     */
    @Override
    public Person updateById(Person person) {

        int index = getPersonById(person.getId());
        if (index >= 0) {
            Person person1 = list.set(index, person);
            return person1;
        } else {
            System.out.println("集合中无此数据，无法修改！");
            return null;
        }
    }

    /**
     * 通过id删除用户
     *
     * @param id
     * @return
     */
    @Override
    public Person deleteById(String id) {
        /**
         * 通过getPersonById方法返回的下标值判断集合中有没有此id，如果有执行删除操作，如果没用返回null值
         */
        int index = getPersonById(id);
        if (index >= 0) {
            Person person = list.get(index);
            list.remove(index);
            return person;
        } else {
            System.out.println("集合中无此数据！");
            return null;
        }
    }
    /**
     * 查看集合的所有信息
     */
    @Override
    public void printIfo() {


    }
}
